// packageK/hxt/addressDetail/addressDetail.js
var o = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    options: [],
    address:[""],
    addressInd:0,
    cascaderValue:"",
    addressDetail:"",

    addressDe:"",//详细地址
    idcard:"",//身份证号
    addressAll:[],
    uid:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.uid = wx.getStorageSync('uid')
    this.getArea()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  selectAddress(){
    this.setData({
      show:!this.data.show
    })
  },
  swichArea(e){
    let { ind , item } = e.currentTarget.dataset
    if(item == "")return
    this.setData({
      addressInd:ind 
    })

    let add = this.data.address
    add.splice(ind+1)
    
    if(ind == 0){
      this.getArea()
      add.push("")
    }else{
      this.getArea(add[ind-1].id , 'tab')
    }
    this.setData({
      address:add
    })
    // console.log(ind);
    // console.log(item);
  },
  getArea(id,type){
    let that = this
    var t = o.getNetAddresss("Baoxian.getArea");
    o._postNetWork({
      url: t,
      data:{
        pid:id
      },
      success(l){
        let i = l.data
        if(i.result == 1){
          if(i.data.length > 0){
            if(id){
              let add = that.data.address.filter(item=>item!="")
              add.push("")
              that.setData({
                options:i.data,
                address:add,
                addressInd:type == 'tab' ? that.data.addressInd :  that.data.addressInd +=1
              })
            }else{
              that.setData({
                options:i.data,
              })
            }
          }else{
            let a = ""
            that.data.address.forEach(item=>{
              a += item.areaname
            })
            that.setData({
              addressDetail:a,
              show:false,
              addressAll:JSON.parse(JSON.stringify(that.data.address))
            })
            // console.log(a);

          }

        }
        // console.log(i);
      }
    })
  },
  selectArea(e){
    let d = e.currentTarget.dataset.item
    let add = this.data.address
    add[this.data.addressInd] = d
    this.setData({
      address:add
    })
    this.getArea(d.id)
    // console.log(e);
  },
  // 身份证号
  getCard(e){
    let {value} = e.detail
    this.setData({
      idcard:value
    })
  },
  getAddressDetail(e){
    let {value} = e.detail
    this.setData({
      addressDe:value
    })
  },
  // 保存
  save(){
    let {idcard,addressDe,uid,addressAll} = this.data
    if(idcard == ""){
      Toast("请输入身份证号")
      return
    }
    if(addressAll.length == 0){
      Toast("请选择身份证地址")
      return
    }
    if(addressDe == ""){
      Toast("请输入身份证详细地址")
      return
    }
    let that = this
    var t = o.getNetAddresss("Baoxian.saveAddress");
    o._postNetWork({
      url: t,
      data:{
        uid:uid,
        address:addressDe,
        province_name:addressAll[0].areaname,
        province:addressAll[0].id,
        city_name:addressAll[1].areaname,
        city:addressAll[1].id,
        area_name:addressAll[2].areaname,
        area:addressAll[2].id,
      },
      success(l){
        let i = l.data
        if(i.result == 1){
          console.log(i);
          wx.setStorageSync('hxtAddress', i.data)
          Toast("保存成功")
          setTimeout(()=>{
            wx.navigateBack({delta:1})
          },500)
        }else{
          Toast(i.msg)
        }
      }
    })
    function Toast(e){
      wx.showToast({
        title: e,
        icon:"none"
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})